//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.iview.service.impl;

import ciicos.base.service.SysconfigService;
import ciicos.iview.service.IviewService;
import ciicos.util.StringUtil;
import cn.hutool.http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IviewServiceImpl implements IviewService {
    private static final Logger log = LoggerFactory.getLogger(IviewServiceImpl.class);
    @Autowired
    private SysconfigService sysConfigApi;

    private String getScadaUrl() {
        return this.sysConfigApi.getSysInfo().getScadaUrl();
    }

    public String userlogin(String user, String pass) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("userlogin no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/userlogin?");
            sb.append("user=" + user);
            sb.append("&pass=" + pass);
            log.debug("请求：" + sb);
            String result = HttpUtil.get(sb.toString());
            log.debug("结果：" + result);
            return result;
        }
    }

    public String userlogout(String token) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("userlogout no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/userlogout?");
            sb.append("token=" + token);
            log.debug("请求：" + sb);
            String result = HttpUtil.get(sb.toString());
            log.debug("结果：" + result);
            return result;
        }
    }

    public String realdata(String token, String names, Integer decimal, Integer valueonly) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("realdata no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/realdata?");
            sb.append("names=" + names);
            if (decimal != null) {
                sb.append("&decimal=" + decimal);
            }

            if (valueonly != null) {
                sb.append("&valueonly=" + valueonly);
            }

            if (StringUtil.isNotEmpty(token)) {
                sb.append("&token=" + token);
            }

            log.debug("请求：" + sb);
            String result = HttpUtil.get(sb.toString());
            log.debug("结果：" + result);
            return result;
        }
    }

    public String realdata(String token, String tags, String pars, Integer decimal, Integer valueonly) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("realdata no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/realdata");
            sb.append("?tags=" + tags);
            sb.append("&pars=" + pars);
            if (decimal != null) {
                sb.append("&decimal=" + decimal);
            }

            if (valueonly != null) {
                sb.append("&valueonly=" + valueonly);
            }

            if (StringUtil.isNotEmpty(token)) {
                sb.append("&token=" + token);
            }

            log.debug("请求：" + sb);
            String result = HttpUtil.get(sb.toString());
            log.debug("结果：" + result);
            return result;
        }
    }

    public String realdir(String token, String subnode, Integer recursion, Integer valueonly, Integer decimal) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("realdir no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/realdir/");
            if (StringUtil.isNotEmpty(subnode)) {
                sb.append(subnode);
            }

            sb.append("?");
            if (recursion != null) {
                sb.append("&recursion=" + recursion);
            }

            if (valueonly != null) {
                sb.append("&valueonly=" + valueonly);
            }

            if (decimal != null) {
                sb.append("&decimal=" + decimal);
            }

            if (StringUtil.isNotEmpty(token)) {
                sb.append("&token=" + token);
            }

            log.debug("请求：" + sb);
            String result = HttpUtil.get(sb.toString());
            log.debug("结果：" + result);
            return result;
        }
    }

    public String realdata(String token, String body) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("realdir no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/realdata");
            if (StringUtil.isNotEmpty(token)) {
                sb.append("?token=" + token);
            }

            log.debug("请求：" + sb);
            String result = HttpUtil.post(sb.toString(), body);
            log.debug("结果：" + result);
            return result;
        }
    }

    public String hisdata(String token, String times, String names, Integer decimal, Integer valueonly) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("realdir no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/hisdata");
            sb.append("?times=" + times);
            sb.append("&names=" + names);
            if (decimal != null) {
                sb.append("&decimal=" + decimal);
            }

            if (valueonly != null) {
                sb.append("&valueonly=" + valueonly);
            }

            if (StringUtil.isNotEmpty(token)) {
                sb.append("&token=" + token);
            }

            log.debug("请求：" + sb);
            String result = HttpUtil.get(sb.toString());
            log.debug("结果：" + result);
            return result;
        }
    }

    public String hisdata(String token, String startTime, String endTime, Integer interval, String names, Integer decimal, Integer valueonly) {
        if (StringUtil.isEmpty(this.getScadaUrl())) {
            log.debug("realdir no scadaurl");
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getScadaUrl() + "/api/hisdata");
            sb.append("?startTime=" + startTime);
            sb.append("&endTime=" + endTime);
            sb.append("&interval=" + interval);
            sb.append("&names=" + names);
            if (decimal != null) {
                sb.append("&decimal=" + decimal);
            }

            if (valueonly != null) {
                sb.append("&valueonly=" + valueonly);
            }

            if (StringUtil.isNotEmpty(token)) {
                sb.append("&token=" + token);
            }

            log.debug("请求：" + sb);
            String result = HttpUtil.get(sb.toString());
            log.debug("结果：" + result);
            return result;
        }
    }
}
